<?php

class ControllerAdmin_Mail extends ControllerAdmin_Base
{
	public $fieldOpt = array(
		'text' => "Текстовое поле",
		'checkbox' => "Чекбокс",
		'select' => "Список",
		'textarea' => "Многострочное поле",
		'radio' => "Радио кнопка"
	);
	
	public function __construct($section) {		
		$this->section      = $section;
		$this->common_vars  = Registry::get("common_vars");
		$this->table_module = PRFX."mail";
	}
	
	public function init()
	{
		$this->pages = new structure($this->table_module);
		$this->pages->active_group = ($_GET["id"] ? $_GET["id"] : 1);
		$this->pages->mode = "mail";
		$this->pages->sorting = ' ORDER BY `Por`';
		$this->pages->field = array(						
			'edit'   => true,
			'por'    => true,
			'show'   => true,
			'delete' => true,			 
			'field_cfg' => array(
				array(
					'name'      => 'Название страницы',
					'fieldname' => 'name',
					'width'     => '50%',
					'link'      => true,			
					'attr'      => false
				),
				array(
					'name'      => 'Системное имя',
					'fieldname' => 'sys_name',
					'width'     => '50%',
					'link'      => true,			
					'attr'      => false
				)
			),
			"aoData" => array(
				"bSortable" => array(
					"false",
					"false",
					"false",
					"false",
					"null",
					"null"
				)
			)
		);
	}
	
	public function addToTable()
	{
		if(!empty($_POST['type']))
		{				
			extract($_POST);			
			if($field) {
				$field = array_values($field);
				$field = serialize($field);
			}
			if($email) {
				$email = array_values($email);
				$email = serialize($email);
			}			
			$date_update = date("Y-m-d H:i:s");
					
			if ($_POST['type'] == 'add') {
				$date_create = date("Y-m-d H:i:s");
				eval(functions::insert_sql($this->table_module, 'sql'));
				$r =& Registry::get("MDB")->query($sql);		
				if (PEAR::isError($r)) {
					die($r->getMessage().' ::: '.$r->getUserInfo());
				}
			}
			elseif($_POST['type'] == 'editt') {
				eval(functions::update_sql($this->table_module, 'sql', 'id', $_POST['id']));				
				$r =& Registry::get("MDB")->query($sql);		
				if (PEAR::isError($r)) {
					die($r->getMessage().' ::: '.$r->getUserInfo());
				}
			}
		}
	}
	
    public function out()
	{
		self::init();		
		self::addToTable();
		
		if(empty($_GET['type'])) {
			$this->action = '<div class="addbuttons"><a href="?m=content&id='.$_GET["id"].'&parent='.$_GET["parent"].'&type=add" class="ui_wo_buttons ui-state-default ui-corner-all"><span class="ui-icon ui-icon-circle-plus"></span>Добавить страницу</a></div>';			
			return $this->pages->FirstPage();
		}		
		else
		{
			if($_GET['type'] == 'edit')
			{							   
				$r =& Registry::get("MDB")->query("SELECT * FROM `".$this->table_module."` WHERE `ID`='".$_GET['pageid']."' LIMIT 1;");
				if (PEAR::isError($r)) {
					die($r->getMessage().' ::: '.$r->getUserInfo());
				}
				if($r->numRows()) {
					$Field = $r->fetchRow(MDB2_FETCHMODE_ASSOC);
				}
				$Field["field"] = __unserialize($Field["field"]);
				$Field["email"] = __unserialize($Field["email"]);	
				
				//print_r($Field["field"]);
			}			
			elseif($_GET['type'] == 'add' || empty($_GET['type'])) {
				$Field["por"] = functions::incrPor($this->table_module);			
			}
			
			/* ID */
			if($this->pages->pInfo['auto_increment'] && empty($_GET['pageid'])) {
				$id_action = $this->pages->pInfo['auto_increment'];
			}
			elseif(!empty($_GET['pageid'])) {
				$id_action = $_GET['pageid'];
			}
			
			$IMAGE = ImgCrop::createUploadCode(array(
			  'text'           => 'Фотогалерея (max 40 фото)',
			  'idelem'         => $id_action,
			  'namefile'       => 'mail_gallery_original.jpg',	 
			  'previews'  => array(
				  array(
					 "namefile" => '450x213_mail_gallery_trumbnail.jpg',
					 "textfile" => 'Превью 450px × 213px',
					 "width"    => '450',
					 "height"   => '213',
					 "prefix"   => '450x213_mail_gallery_trumbnail'
				  )  
			  ),
			  'folder'         => "mail",
			  'limit'          => 40,
			  'prefix'         => 'mail_gallery_original',
			  'processor'      => '/ajax/admin/upload.backend.php'
			))."<br /><hr />";
			
			//print_r($this->common_vars);
			Registry::get("smarty")->assign(array(
				'SECTION'   => $this->section,				
				'ID_ACTION' => $id_action,
				'Field'     => $Field,
				'FieldOpt'  => $this->fieldOpt,
				'TABLE'     => $this->table_module,
				'IMAGE'     => $IMAGE
			));
			return Registry::get("smarty")->fetch(TEMPLATE_ADMIN.$this->common_vars["controller_template"], Registry::get("cache_id"));
		}		
    }
}

$controller = new ControllerAdmin_Mail($section);
$ob_admin_out = $controller->out();
$ob_admin_action = $controller->action;

?>